﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;

namespace TradeMarkSys
{
    public partial class FrmChangePWD : Form
    {
        public FrmChangePWD()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = null;
            try
            {
                if(txtConfirmPWD.Text.Equals(""))
                {
                    MessageBox.Show("密码不能为空,请重试!");
                    return;
                }
                if (!txtConfirmPWD.Text.Equals(txtNewPWD.Text))
                {
                    MessageBox.Show("两次输入的密码不一致,请重试!");
                    return;
                }
                string sql = string.Format(@"select * from TS_User where UserName = '{0}'", FrmMain.CurrentUser.UserName);
                string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                conn = new OleDbConnection(connstr);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string oldpwd = ds.Tables[0].Rows[0]["PassWord"].ToString();
                    if (!txtOldPWD.Text.Equals(oldpwd))
                    {
                        MessageBox.Show("原密码不正确,请重试!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("用户数据不存在,请退出系统重新登录!");
                    return;
                }
                sql = string.Format("update TS_User set [PassWord] = '{0}' where [UserName] = '{1}'", txtNewPWD.Text, FrmMain.CurrentUser.UserName);
                cmd = new OleDbCommand(sql, conn);
                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("保存成功!");
                }
                else
                {
                    MessageBox.Show("保存失败,请重试!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}
